package common.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: 葛成瑞
 * @Date: 2021/6/4 16:37
 */
public class YouDaoTransLate {


    //中转英
    public static String DoCnToEnTrans(String str) {
        if (StringUtils.isNotEmpty(str)) {
            if (str.contains("|")) {
                str = str.split("\\|")[1];
            }
            str = str.replaceAll("<[^>]+>", "");
            Map<String, Object> paramMap = new LinkedHashMap<>();
            paramMap.put("q", str);
            paramMap.put("from", "zh-CHS");
            paramMap.put("to", "en");
            String body = HttpUtil.post("http://10.1.1.17:18081/v2/translate_a", paramMap);
            JSONObject object = JSONObject.parseObject(body);
            if (object.getString("errorCode").equals("0")) {
                String translationResults = object.getJSONArray("translationResults").getJSONObject(0).getString("translation");
                return translationResults;
            } else {
                return "";
            }
        } else {
            return "";
        }

    }

    public static void main(String[] args) {
        System.out.println(DoEnToCnTrans("HELLO"));
    }

    //英转中
    public static String DoEnToCnTrans(String str) {
        if (StringUtils.isNotEmpty(str)) {
            str = str.replaceAll("<[^>]+>", "");
            System.out.println("英转中+++" + str);
            Map<String, Object> paramMap = new LinkedHashMap<>();
            paramMap.put("q", str);
            paramMap.put("from", "en");
            paramMap.put("to", "zh-CHS");
            String body = HttpUtil.post("https://fanyi.yuntsg.com/v2/translate_a", paramMap);
            return body;
//            JSONObject object = JSONObject.parseObject(body);
//            if (object.getString("errorCode").equals("0")) {
//                String translationResults = object.getJSONArray("translationResults").getJSONObject(0).getString("translation");
//                System.out.println("结果+++" + translationResults + "+++++++");
//                if (!StringUtils.isNotEmpty(translationResults)) {
//                    for (int i = 0; i < 3; i++) {
//                        String transRes = getTransRes(str);
//                        if (StringUtils.isNotEmpty(transRes)) {
//                            return translationResults;
//                        }
//                    }
//                }
//                return translationResults;
//            } else {
//                return "";
//            }
        } else {
            return "";
        }
    }

    //循环获取有道翻译 高峰期获取不到情况 2021年9月18日10:27:30
    public static String getTransRes(String str) {
        str = str.replaceAll("<[^>]+>", "");
        Map<String, Object> paramMap = new LinkedHashMap<>();
        paramMap.put("q", str);
        paramMap.put("from", "en");
        paramMap.put("to", "zh-CHS");
        String body = HttpUtil.post("http://123.128.12.90:18081/v2/translate_a", paramMap);
        JSONObject object = JSONObject.parseObject(body);
        if (object.getString("errorCode").equals("0")) {
            String translationResults = object.getJSONArray("translationResults").getJSONObject(0).getString("translation");
            return translationResults;
        } else {
            return "";
        }

    }
}
